package il.ac.tau.dbcourse.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

public class Dialog {
	final Shell dialog;
	final boolean [] result;
	public Dialog(Shell parentshell,String text){
		result = new boolean [1];
		dialog = new Shell (parentshell, SWT.DIALOG_TRIM | SWT.APPLICATION_MODAL);
		//dialog.setLayout (new RowLayout ());
		GridLayout grid=new GridLayout();
		grid.horizontalSpacing=10;
		grid.makeColumnsEqualWidth=true;
		grid.numColumns=1;
		grid.verticalSpacing=10;
		grid.marginWidth=10;
		grid.marginHeight=10;
		
		
		dialog.setLayout (grid);
		final Label label = new Label(dialog, SWT.None);
		label.setText(text);
		final Button ok = new Button (dialog, SWT.PUSH);
		ok.setText ("OK");
		Button cancel = new Button (dialog, SWT.PUSH);
		cancel.setText ("Cancel");
		Listener listener =new Listener () {
			public void handleEvent (Event event) {
				result [0] = event.widget == ok;
				dialog.close ();
			}
		};
		ok.addListener (SWT.Selection, listener);
		cancel.addListener (SWT.Selection, listener);
		
		cancel.setLayoutData(new GridData(GridData.FILL_BOTH));
		cancel.setSize(70, cancel.getSize().y);
		ok.setLayoutData(new GridData(GridData.FILL_BOTH));
		ok.setSize(cancel.getSize());
		
	}
	
	public Boolean getResult(){
		dialog.pack ();
		dialog.open ();
		while (!dialog.isDisposed ()) {
			if (!dialog.getDisplay().readAndDispatch ()) dialog.getDisplay().sleep ();
		}
		return result[0];
	}
	
}
